package com.dragon.logical.service.website.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.website.IDisplayMaterialService;
import com.dragon.repository.entity.DisplayMaterial;
import com.dragon.repository.mapper.DisplayMaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 材料展示 服务实现类
 * </p>
 *
 * @author sy
 * @since 2024-03-01
 */
@Service
public class DisplayMaterialServiceImpl extends ServiceImpl<DisplayMaterialMapper, DisplayMaterial> implements IDisplayMaterialService {

    @Autowired
    DisplayMaterialMapper displayMaterialMapper;

    @Override
    public List<DisplayMaterial> materialList() {
        return displayMaterialMapper.selectList(null);
    }

    @Override
    public void materialAdd(DisplayMaterial displayMaterial) {
        displayMaterial.setCreateTime(DateUtil.date());
        displayMaterialMapper.insert(displayMaterial);
    }

    @Override
    public void materialUpdate(DisplayMaterial displayMaterial) {
        displayMaterial.setUpdateTime(DateUtil.date());
        displayMaterialMapper.updateById(displayMaterial);
    }

    @Override
    public void materialDeleted(Integer id) {
        DisplayMaterial displayMaterial = displayMaterialMapper.selectById(id);
        if (displayMaterial == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        displayMaterialMapper.deleteById(id);
    }
}
